<?php

namespace App\Admin\Controllers;

use App\Models\Gamelog;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Layout\Row;
use Encore\Admin\Widgets\Collapse;
use Encore\Admin\Widgets\Table;
use Illuminate\Http\Request;

class StatisticController extends Controller
{
    use ModelForm;
    protected $type;
    /**
     * Index interface.
     *
     * @return Content
     */
    public function index(Request $request)
    {
        $this->type = $request->type;
        return Admin::content(function (Content $content) {

            $content->header('统计面板');
            $content->description('description');
            $rank = $this->getRank();
            $student = [];
            $counter = [];
            $ids = [];
            $incorrect = [];
            foreach ($rank->toArray() as $item){
               array_push($student, $item['name']);
               array_push($counter, $item['prop']);
               array_push($ids, $item['id']);
               array_push($incorrect, $item['incorrect']);
            };
           $header = ['排名', '名字', '正确率', '错误次数'];
           $rows = [];
           for($i = 0; $i < count($student); $i ++)
           {
               $item = [$i + 1, $student[$i], $counter[$i], $incorrect[$i]];
                array_push($rows, $item);
           }
           $table = new Table($header, $rows, [

           ]);
           $content->row(function (Row $row) use ($table){
               $collapse = new Collapse();
               $collapse->add('正确率排行(前三十)', $table);
                $row->column(6, $collapse->render());
                $count = $this->getSex();
                $men_count = $count['men'];
                $women_count = $count['women'];
                $row->column(6, view('admin.charts.sex',compact('women_count', 'men_count')));
            });
        });
    }

    protected function getList()
    {
        $logs = Gamelog::where('game_type', $this->type)->get(['game_data','student_id']);
        $list = [];
        foreach ($logs as $log)
        {
            array_push($list, $this->getStatistic($log));
        }
        return collect($list)->flatten(1);
    }

    protected function getStatistic(Gamelog $gamelog)
    {
        $list = [];
        $data = json_decode($gamelog->game_data, true);

        foreach ($data as $item)
            {
                $gamelog->question_id = $item['question_id'];
                $gamelog->isCorrect = $item['isCorrect'];
                $gamelog->name = optional($gamelog->student)->name;
                $gamelog->age = optional($gamelog->student)->age;
                $gamelog->sex = optional($gamelog->student)->sex;
                unset($gamelog->student);
                unset($gamelog->game_data);
                array_push($list, $gamelog->toArray());
            }
            $items = [];
        for ($k = 0; $k < count($list); $k ++)
        {
            array_push($items, $list[$k]);
        }
        return $items;
    }

    protected function getStudents()
    {

      $list = $this->getList();
        $studentList = [];
        foreach ($list as $item)
            array_push($studentList, $item['student_id']);
        return array_unique($studentList);
    }

    protected function getRank()
    {
        $students = $this->getStudents();
        $list = $this->getList();
        $rank = [];
        foreach ($students as $student){
            $arr = collect($list)->where('student_id', $student);
            $name = $arr->first();
            $correct = $arr->where('isCorrect', 1)->count();
            $prop = $correct / $arr->count() *100;
            $incorrect = $arr->count() - $correct;
            $arr = [];
            $arr['name'] = $name['name'];
            $arr['prop'] = round($prop);
            $arr['id'] = $name['student_id'];
            $arr['incorrect'] = $incorrect;
            array_push($rank, $arr);
        }
        return collect($rank)->take(30)->sortByDesc('prop');
    }

    protected function getSex()
    {
        $students = $this->getStudents();
        $list = $this->getList();
        $rank = [];
        $men = 0;
        foreach ($students as $student) {
            $arr = collect($list)->where('student_id', $student);
            $count = $arr->where('sex', 1)->count();
            if ($count){
                $men ++;
            }
        }
        $women = count($students)-$men;
        return [
            'women' => $women,
            'men' => $men
        ];
    }
}

